package tt.dz.dao.ext.station;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import tt.dz.dao.base.BaseDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.station.OmsStationExternal;
import tt.dz.util.StringUtil;

/**
 * 外部电站基础信息数据操作Dao
 * 
 * @author pengyu
 * 
 */
@Repository
public class OmsStationExternalDao extends BaseDao<OmsStationExternal> {

	@SuppressWarnings("rawtypes")
	public JsonForList getStationList(OmsStationExternal station) {
		List<OmsStationExternal>  list = new ArrayList<OmsStationExternal>();
		String code,msg;
		StringBuilder sb = new StringBuilder(" where 1 = 1 ");
		if(station!=null){
			if(StringUtil.isNotBlank(station.getStation_id())){
				sb.append(" and station_id =  '"+station.getStation_id()+"'");
			}if(station.getValid_flag()!=null){
				sb.append(" and valid_flag =  "+station.getValid_flag());
			}if(StringUtil.isNotBlank(station.getStation_name())){
				sb.append(" and station_name like  '%"+station.getStation_name()+"%'");
			}if(StringUtil.isNotBlank(station.getDistrict_code())){
				sb.append(" and district_code like  '%"+station.getDistrict_code()+"%'");
			}
		}
		list = findListByCondition(new OmsStationExternal(), sb.toString());
        code ="200";
        msg="ok";
        JsonForList<OmsStationExternal>  jList = new  JsonForList<OmsStationExternal>(); 
        jList.setCode(code);
        jList.setMsg(msg);
        jList.setList(list);
		return jList;
	}

	@SuppressWarnings("rawtypes")
	public JsonForList addStation(OmsStationExternal station) {
		JsonForList json = new JsonForList();
		//TODO  按规则生成id
		try {
			save(station);
			json.setCode("200");
			json.setMsg("save success");
			return json;
		} catch (Exception e) {
			json.setCode("400");
			json.setMsg("save Exception");
			e.printStackTrace();
			return json;
		} 
	}

	@SuppressWarnings("rawtypes")
	public JsonForList updateStation(OmsStationExternal station) {
		JsonForList json = new JsonForList();
		try {
			updateObject(station);
			json.setCode("200");
			json.setMsg("update success");
			return json;
		} catch (Exception e) {
			json.setCode("400");
			json.setMsg("update Exception");
			e.printStackTrace();
			return json;
		} 
	}

	@SuppressWarnings("rawtypes")
	public JsonForList deleteStation(List<String> list) {
		JsonForList json = new JsonForList();
		StringBuilder sb =new StringBuilder("update oms_station_external set valid_flag = 0 where 1 = 1 ");
		if(list.size()<=0){
			json.setCode("400");
			json.setMsg("stationIdList null");
			return json;
		}
		sb.append(" and station_id in (");
		for(String id :list){
			sb.append("'"+id+"',");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")");
		try {
			exec(sb.toString());
			json.setCode("200");
			json.setMsg("loginDelStation success");
			return json;
		} catch (Exception e) {
			json.setCode("400");
			json.setMsg("loginDelStation Exception");
			e.printStackTrace();
			return json;
		} 
	}

}
